<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Live extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(!session('name')){
            $this->redirect("/admin/login/login");
        }
    }
    public function index(){
        $liveList=Db::name('live')->where('live_end_time','>',time())->select();
        $this->assign('liveList',$liveList);
        return $this->fetch();
    }
    public function add(){
        if(request()->isPost()){
            $data=(input('param.'));
            $checkDate=new \app\admin\Validate\Live();
            if(!$checkDate->check($data)){
                return $this->error($checkDate->getError());
            }
            $endTime= strtotime(date($data['live_end_time']));
            if($endTime<=time()){
                return $this->error("结束时间应大于当前时间");
            }
            $checkName=(new \app\common\model\Live())->checkName($data['live_name']);
            if($checkName!=0){
                return $this->error('直播名称已存在，请使用其他名称');
            }
            $data['live_end_time']=$endTime;
            $data['live_start_time']=time();
            $livename=rand(1000,9999);
            $url=$this->getPushUrl("97868.livepush.myqcloud.com",$livename,"c81ff6cbbe5a3a9f42eded8d8cf0b0c2","2020-09-11 20:08:07");
            $data['live_url']=$url['url'];
            $data['live_play_url']=$url['play_url'];
            $res=(new \app\common\model\Live())->save($data);
            $liveRes=Db::name('livereserve')->where('livereserve_state',1)->select();
            foreach ($liveRes as $v){
                $phone=Db::name('user')->where('user_id',$v['livereserve_user_id'])->value('user_name');
                $send=$this->sendLiveSms($phone);
            }
            Db::name('livereserve')->where('livereserve_state',1)->update(['livereserve_state'=>0]);
            if($res){
                return $this->success('直播添加成功','admin/live/index');
            }else{
                return $this->error('直播添加失败');
            }
        }
        return $this->fetch();
    }
    function sendLiveSms($phone){
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        $smsapi = "http://api.smsbao.com/";
        $user = "shiguotai"; //短信平台帐号
        $pass = md5("000000sgt"); //短信平台密码
        $content="【MHK学习平台】您的验证码是".rand(1000,9999).",马上登陆观看吧";
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendurl) ;
    }
    function getPushUrl($domain, $streamName, $key = null, $time = null){
        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            //txSecret = MD5( KEY + streamName + txTime )
            $txSecret = md5($key.$streamName.$txTime);
            $ext_str = "?".http_build_query(array(
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
        }
        $res['url']="rtmp://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");
        $res['play_url']="http://live.zytec.top/live/".$streamName.".m3u8".$ext_str;
        return $res;
    }
    public function delete(){
        $live_id=input('param.id');
        $res=Db::name('live')->delete($live_id);
        if($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
    public function livereserve(){
        $reserveList=Db::name('livereserve')->select();
        foreach ($reserveList as $k=>$v){
            $reserveList[$k]['user']=Db::name('user')->where('user_id',$v['livereserve_user_id'])->find();
        }
        $this->assign('reserveList',$reserveList);
        return $this->fetch();
    }
}
